﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;

namespace emp.Utility
{
    public static class TraceLog
    {
        private static bool _newLine = true;
        public static bool WriteToFile = false;

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="ex">程序执行期间发生的错误</param>
        public static void RecordLog(Exception ex)
        {
            StreamWriter writer;
            string path = ConfigurationManager.AppSettings["LogPath"];
            if (!File.Exists(path))
            {
                try
                {
                    using (writer = File.CreateText(path))
                    {
                        writer.WriteLine("---------------new error---------------");
                        writer.WriteLine("-----DateTime:-----" + DateTime.Now);
                        writer.WriteLine("-----Message:------" + ex.Message);
                        writer.WriteLine("-----TargetSite:---" + ex.TargetSite);
                        writer.WriteLine("-----Source:-------" + ex.Source);
                        writer.WriteLine("");
                        writer.Close();
                    }
                }
                catch
                {
                }
            }
            else
            {
                try
                {
                    using (writer = File.AppendText(path))
                    {
                        writer.WriteLine("---------------new error---------------");
                        writer.WriteLine("-----DateTime:-----" + DateTime.Now);
                        writer.WriteLine("-----Message:------" + ex.Message);
                        writer.WriteLine("-----TargetSite:---" + ex.TargetSite);
                        writer.WriteLine("-----Source:-------" + ex.Source);
                        writer.WriteLine("");
                        writer.Close();
                    }
                }
                catch
                {
                }
            }
        }

        public static void RecordLog(string tstr)
        {
            StreamWriter writer;
            string path = ConfigurationManager.AppSettings["LogPath"] + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            if (!File.Exists(path))
            {
                try
                {
                    using (writer = File.CreateText(path))
                    {
                        writer.WriteLine("-----DateTime:-----" + DateTime.Now);
                        writer.WriteLine("-----Message:------" + tstr);
                        writer.WriteLine("");
                        writer.Close();
                    }
                }
                catch
                {
                }
            }
            else
            {
                try
                {
                    using (writer = File.AppendText(path))
                    {
                        writer.WriteLine("-----DateTime:-----" + DateTime.Now);
                        writer.WriteLine("-----Message:------" + tstr);
                        writer.WriteLine("");
                        writer.Close();
                    }
                }
                catch
                {
                }
            }
        }

        // Methods
        public static void Error(string format, params object[] args)
        {
            string message = GetMessage(format, args);
            DateTime time = DateTime.Now.ToLocalTime();
            if (!_newLine)
            {
                WriteToLogFile("\r\n");
            }
            string str2 = string.Format("[{0}]: {1}", time, message);
            WriteToLogFile(str2 + "\r\n");
            _newLine = true;
        }

        private static string GetMessage(string format, object[] args)
        {
            if (args.Length > 0)
            {
                return string.Format(format, args);
            }
            return format;
        }

        private static void Print(string format, params object[] args)
        {
            string message = GetMessage(format, args);
            DateTime time = DateTime.Now.ToLocalTime();
            if (_newLine)
            {
                string str2 = string.Format("[{0}]: ", time);
                WriteToLogFile(str2);
                _newLine = false;
            }
            WriteToLogFile(message);
        }

        public static void PrintLn(string format, params object[] args)
        {
            Print(format, args);
            WriteToLogFile("\r\n");
            _newLine = true;
        }

        private static void WriteToLogFile(string str)
        {
            if (WriteToFile && !string.IsNullOrEmpty(str))
            {
                str = str.Replace("\b", "");
                string path = ConfigurationManager.AppSettings["LogPath"];
                StreamWriter writer = new StreamWriter(path, true);
                writer.Write(str);
                writer.Flush();
                writer.Close();
            }
        }
    }
}
